<template>
  <view 
    v-if="visible" 
    class="sheet-modal-overlay"
    :style="{ zIndex: computedZIndex }"
    @click="handleMaskClick"
  >
    <view 
      class="sheet-modal-container"
      :class="{ 'sheet-show': visible }"
      :style="containerStyle"
      @click.stop
    >
      <!-- 标题区域 -->
      <view v-if="title" class="sheet-header">
        <text class="sheet-title">{{ title }}</text>
        <button v-if="showClose" class="sheet-close" @click="handleClose">
          <text class="close-icon">✕</text>
        </button>
      </view>
      
      <!-- 内容区域 -->
      <view class="sheet-body">
        <slot></slot>
      </view>
    </view>
  </view>
</template>

<script>
import modalManager from '../utils/modal-manager.js'

export default {
  name: 'SheetModal',
  
  props: {
    // 显示状态
    visible: {
      type: Boolean,
      default: false
    },
    
    // 标题
    title: {
      type: String,
      default: ''
    },
    
    // 最大高度
    maxHeight: {
      type: String,
      default: '60vh'
    },
    
    // 点击遮罩是否关闭
    maskClosable: {
      type: Boolean,
      default: true
    },
    
    // 显示关闭按钮
    showClose: {
      type: Boolean,
      default: false
    },
    
    // 优先级
    priority: {
      type: String,
      default: 'normal'
    },
    
    // 背景颜色
    backgroundColor: {
      type: String,
      default: '#ffffff'
    }
  },
  
  data() {
    return {
      modalId: `sheet_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
      computedZIndex: 1000
    }
  },
  
  computed: {
    containerStyle() {
      return {
        maxHeight: this.maxHeight,
        backgroundColor: this.backgroundColor
      }
    }
  },
  
  watch: {
    visible(newVal) {
      if (newVal) {
        this.computedZIndex = modalManager.register(this.modalId, this.priority)
      } else {
        modalManager.unregister(this.modalId)
      }
    }
  },
  
  mounted() {
    if (this.visible) {
      this.computedZIndex = modalManager.register(this.modalId, this.priority)
    }
  },
  
  beforeUnmount() {
    modalManager.unregister(this.modalId)
  },
  
  methods: {
    handleMaskClick() {
      if (this.maskClosable) {
        this.handleClose()
      }
    },
    
    handleClose() {
      this.$emit('update:visible', false)
      this.$emit('close')
    }
  }
}
</script>

<style scoped>
.sheet-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  backdrop-filter: blur(4rpx);
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.sheet-modal-container {
  width: 100%;
  background: white;
  border-radius: 20rpx 20rpx 0 0;
  box-shadow: 0 -4rpx 32rpx rgba(0, 0, 0, 0.2);
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.sheet-modal-container.sheet-show {
  transform: translateY(0);
}

.sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.sheet-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  flex: 1;
}

.sheet-close {
  width: 56rpx;
  height: 56rpx;
  border-radius: 50%;
  background: #f5f5f5;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sheet-close:active {
  background: #e0e0e0;
}

.close-icon {
  font-size: 24rpx;
  color: #666;
  font-weight: bold;
}

.sheet-body {
  padding: 24rpx;
  overflow-y: auto;
}

.sheet-body::-webkit-scrollbar {
  width: 6rpx;
}

.sheet-body::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 3rpx;
}
</style> 